package com.example.treinamentotiroaoalvo;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class TargetBehaviour
{
	public Bitmap target;
	Paint paint;
	public float speedY = 0.3f;
	public float speedX = 0.3f;
	public float posY;
	public float posX;
	public long startPoint = 0;
	
	Random random = new Random();	
	int index;
	
	Rect rect;
	
	public TargetBehaviour()
	{
		paint = new Paint();
		paint.setColor(Color.BLACK);
		index = random.nextInt(8);
		rect = new Rect();

		target = BitmapStorage.getInstance().getTargetImage();
		
		if(index == 0)
		{
			posX = (GameLoop.canvasWidth/2) - (target.getWidth()/2);
		}
		
		if(index == 1)
		{
			posY = (GameLoop.canvasHeight/2) - (target.getHeight()/2);
		}
		
		if(index == 2)
		{
			posX = (GameLoop.canvasWidth/2) - (target.getWidth()/2);
			posY = GameLoop.canvasHeight - target.getHeight();
		}
		
		if(index == 3)
		{
			posY = (GameLoop.canvasHeight/2) - (target.getHeight()/2);
			posX = GameLoop.canvasWidth - target.getWidth();
		}
		
		if(index == 4)
		{
			posY = (GameLoop.canvasHeight/2 - (target.getHeight()/2));
			speedY = 0.43f;
			speedX = 0.3f;
		}
		
		if(index == 5)
		{
			posY = (GameLoop.canvasHeight/2) - (target.getHeight()/2);
			posX = GameLoop.canvasWidth - target.getWidth();
			speedY = -0.43f;
			speedX = -0.3f;
		}
		
		if(index == 6)
		{
			posX = (GameLoop.canvasWidth/2 - (target.getWidth()/2));
			speedY = 0.17f;
			speedX = 0.5f;
		}
		
		if(index == 7)
		{
			posX = (GameLoop.canvasWidth/2) - (target.getWidth()/2);
			posY = GameLoop.canvasHeight - target.getHeight();
			speedY = -0.17f;
			speedX = -0.5f;
		}
		
		speedX *= 2;
		speedY *= 2;
	}
	
	public void Update()
	{
		rect.set((int)posX, (int)posY, (int)(posX + target.getWidth()), (int)(posY + target.getHeight()));
		
		if(index == 0)
		{
			posY += speedY;
		}
		
		if(index == 1)
		{
			posX += speedX;
		}
		
		if(index == 2)
		{
			posY -= speedY;
		}
		
		if(index == 3)
		{
			posX -= speedX;
		}
		
		if(index == 4 || index == 5)
		{
			posY += speedY;
			posX += speedX;
			
			if(posY < 0 || posY > GameLoop.canvasHeight - (target.getHeight()))
			{
				speedY = -speedY;
			}
		}
		
		if(index == 6 || index == 7)
		{
			posX += speedX;
			posY += speedY;
			
			if(posX < 0 || posX > GameLoop.canvasWidth - (target.getWidth()))
			{
				speedX = -speedX;
			}
		}
	}
	
	public void draw(Canvas canvas)
	{
		if(target != null)
		{
			canvas.drawBitmap(target,null,rect,paint);
		}
	}
	
	public Rect getTargetRect()
	{
		return rect;
	}
}
